<template>
  <div>
    <el-button type="primary" size="small" @click="addMenu"><i class="el-icon-plus"></i> 新增</el-button>
    <el-table ref="list" :data="tableData" style="width: 100%; margin-bottom: 5px;" border row-key="id"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" :header-cell-style="{ textAlign: 'center' }">
      <!-- <el-table-column type="selection" fixed width="50px">
      </el-table-column> -->
      <el-table-column prop="nameZh" label="菜单名称" width="180px">
      </el-table-column>
      <el-table-column prop="iconCls" label="图标" width="80px" align="center">
        <template slot-scope="scope">
          <i :class="scope.row.iconCls"></i>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="name" label="路由名称" width="100px">
      </el-table-column> -->
      <el-table-column prop="path" label="路由地址" width="160px">
      </el-table-column>
      <el-table-column prop="name" label="路由名称" width="160px">
      </el-table-column>
      <el-table-column prop="component" label="组件路径">
      </el-table-column>
      <el-table-column prop="priority" label="优先级" width="80px" align="center">
      </el-table-column>
      <el-table-column prop="show" label="状态" width="80px" align="center">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.show" active-color="#13ce66" inactive-color="#ff4949"
            @change="selectStatus(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180" fixed="right" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="rowEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" class="margin-l-25" @click="rowDel(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <menu-manage-add v-if="isShowAddDialog" :isShowAddDialog="isShowAddDialog"
      @dialogClose="dialogClose"></menu-manage-add>
    <menu-manage-edit v-if="isShowEditDialog" :isShowEditDialog="isShowEditDialog" :record="formFileds"
      @dialogClose="dialogClose"></menu-manage-edit>
  </div>
</template>

<script>
import { getAll, delMenu, updateMenu, show, hide } from "@/api/systemManage/menuManage";
import MenuManageEdit from './menuManageEdit.vue';
import MenuManageAdd from './menuManageAdd.vue';
export default {
  name: "menuMange",
  components: {
    MenuManageEdit,
    MenuManageAdd
  },
  data () {
    return {
      tableData: [],
      formFileds: {},
      isShowEditDialog: false,
      isShowAddDialog: false
    }
  },
  methods: {
    getList () {
      getAll().then((res) => {
        this.tableData = res.data
      })
    },
    dialogClose () {
      this.isShowEditDialog = false
      this.isShowAddDialog = false
    },
    rowEdit (row) {
      this.formFileds = row
      this.isShowEditDialog = true
    },
    addMenu () {
      this.isShowAddDialog = true
    },
    selectStatus (row) {
      // updateMenu(row).then((res) => {
      //   this.getList
      // })
      if (row.show == true) {
        this.showMenu(row)
      } else {
        this.hideMenu(row)
      }
    },
    showMenu (row) {
      show('id=' + row.id).then((res) => {
        if (row.children.length > 0) {
          row.children.show = true
          this.showMenu(row.children)
        } else {
          return
        }
      })
    },
    hideMenu (row) {

      hide('id=' + row.id).then((res) => {
        if (row.children.length > 0) {
          row.children.show = false
          this.hideMenu(row.children)
        } else {
          return
        }
      })

    },
    rowDel (id) {
      this.$confirm("确定要删除当前行吗？", "删除", {
        comfirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(() => {
        delMenu('id=' + id).then((res) => {
          if (res) {
            this.$message.success("删除成功！");
            this.$router.go(0)
          }
        });
      })
    }
  },
  mounted () {
    this.getList();
  }
}
</script>

<style scoped lang="less">

</style>